package com.symbio.ishenapi.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.bean.LoginUser;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.order.ExcelExport.ExcelExportDoctorUtil;
import com.symbio.ishenapi.order.model.TmOrderDictionary;
import com.symbio.ishenapi.order.model.TmServiceOrder;
import io.jboot.web.controller.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务
 */
@RequestMapping("/TmDoctorOrders")
public class TmDoctorOrdersController extends BaseController {

    /**
     * wy: 医生结算列表分页展示
     */
    public void findPageAll(){
        Map map = TmServiceOrder.dao.findPageAll(getQueryModel());
        if (null != map && map.size() > 0){
            renderPageJsonRet(map.get("count"),map.get("data"));
        }else {
            renderJsonError(40003,"请重试!");
        }
    }

    /**
     * wy: 查询某个医生订单
     */
    public void findDoctorPageAll(){
        TmServiceOrder t = new TmServiceOrder();
        String doctorId = getPara("doctorId");
        String orderType = getPara("orderType");
        String reList = getPara("reList");
        String orderStatus = getPara("orderStatus");
        String doctorName = getPara("doctorName");
        String doctorTEL = getPara("doctorTEL");
        String orderServiceType = getPara("orderServiceType");
        Date begin = getParaToDate("begin");
        Date end = getParaToDate("end");
        if (doctorId != null && !"".equals(doctorId)){
            t.setDoctorId(new BigInteger(doctorId));
        }
        t.setOrderStatus(orderStatus);
        t.setOrderType(orderType);
        t.setDoctorName(doctorName);
        t.setDoctorName(doctorTEL);
        t.setDoctorName(orderServiceType);

        LoginUser loginUser = getLoginUser();
        String Ids = loginUser.getDoctorIds();
        if (Ids.endsWith(",")) {
            Ids = Ids.substring(0,(Ids.length() - 1));
        }

        Map map = TmServiceOrder.dao.findDoctorPageAll(doctorId,orderType,orderStatus,doctorTEL,orderServiceType,doctorName, begin, end,getQueryModel(),Ids,reList);
        if (null != map && map.size() > 0){
            renderPageJsonRet(map.get("count"),map.get("data"));
        }else {
            renderJsonError(40003,"请重试!");
        }
    }

    /**
     * wy: 医生结算按钮
     */
    public void doctorAccount(){
        TmServiceOrder t = (TmServiceOrder) getReqBodyObject(TmServiceOrder.class);
        if (TmServiceOrder.dao.doctorAccount(t)){
            renderJsonRet(40001);
        }else {
            renderJsonError(40002,"结算");
        }
    }

    /**
     * wy: 医生退费按钮
     */
    public void doctorReList(){
        TmServiceOrder t = (TmServiceOrder) getReqBodyObject(TmServiceOrder.class);
        t.setReList("0");
        if (TmServiceOrder.dao.doctorReList(t)){
            renderJsonRet(40001);
        }else {
            renderJsonError(40002,"退费");
        }
    }

    /**
     * wy:查询订单类型等下拉列表
     */
    public void findOrderCode(){
        String dictionaryType = getPara("dictionaryType");
        TmOrderDictionary tod = new TmOrderDictionary();
        List<Record> orderCode = tod.findOrderCode(dictionaryType);
        if (orderCode != null && !"".equals(orderCode)){
            renderJsonRet(orderCode);
        }else {
            renderJsonError(500002);
        }

    }

    /**
     *wy: 导出医生结算Excel
     */
    public void exportDoctor() {
        HttpServletResponse response = getResponse();
        List<TmServiceOrder> list = TmServiceOrder.dao.exportDoctor();
        String sheetName = "医生结算信息";
        String titleName = "医生结算信息";
        String fileName = "医生结算信息";
        int columnNumber = 8;
        int[] columnWidth = {20, 15,50,15,20,15,15,15};
        String[] columnName = { "医生手机号", "医生名称", "医院","职称","创建时间","服务费","药费","总计" };
        ExcelExportDoctorUtil expor = new ExcelExportDoctorUtil();
        String str = "";
        try {
            str = expor.ExportNoResponse(sheetName, titleName, fileName,
                    columnNumber, columnWidth, columnName, list,response);
            renderJsonRet(500001);
        }catch (Exception e){

            e.printStackTrace();
        }
    }

    /**
     * wy: 根据医生ID和订单类型查询患者ID
     */
    public void findDidAndTypeUser(){
        TmServiceOrder t = (TmServiceOrder) getReqBodyObject(TmServiceOrder.class);
        if (t.getDoctorId() != null && !"".equals(t.getDoctorId()) && t.getOrderType() != null && !"".equals(t.getOrderType())){
            List<TmServiceOrder> user = t.findDidAndTypeUser(t);
            String userIds = "";
            for (int i = 0; i < user.size(); i++ ){
                BigInteger userId = user.get(i).getUserId();
                userIds += userId.toString();
                if (++i < user.size()){
                    userIds += ",";
                }
            }
            renderJsonRet(userIds);
        }else {
            renderJsonError(40002,"结算信息");
        }
    }

    /**
     * wy:患者端
     * 根据医生ID,患者ID,服务类型查询已支付,但未完成的订单
     */
    public void findByIdEndServiceTypeToList(){
        TmServiceOrder tso = (TmServiceOrder)getReqBodyObject(TmServiceOrder.class);
        List<TmServiceOrder> list = tso.findByIdEndServiceTypeToList(tso);
        if (list != null && list.size() > 0){
            renderJsonRet(list);
            return;
        }else {
            renderJsonError(40003,"订单");
            return;
        }
    }

    /**
     * wy:患者端
     * 根据医生ID,患者ID,查询已支付,但未完成的订单
     */
    public void findByIdToList(){
        TmServiceOrder tso = (TmServiceOrder)getReqBodyObject(TmServiceOrder.class);
        List<Record> count = tso.findByIdToList(tso);
        if (count != null && count.size() > 0){
            renderJsonRet(count);
            return;
        }else {
            renderJsonError(50002,"订单查询");
            return;
        }
    }

    /**
     * wy:患者端
     * 根据订单ID查询 医生ID,患者ID
     */
    public void findByIdToDidAndPid(){
        String id = getPara("id");
        TmServiceOrder order = TmServiceOrder.dao.findById(id);
        Map map = new HashMap();
        if (order != null && !"".equals(order)){
            map.put("doctorId",order.getDoctorId());
            map.put("patientId",order.getUserId());
            renderJsonRet(map);
            return;
        }else {
            renderJsonError(50002,"查询");
            return;
        }
    }

}
